
$(document).ready(function(){
	pub.main();
});

var pub = {
	data : {
		navSwich : true,
		headerAniNum:""
	},
	url : {
		loginUrl : homeUrl+"/pass/login.html",
		imgUrl : protocol+"//images.iyunmai.com/home/" //protocol+"//images.iyunmai.com/home/"
	},
	main : function(){
		//pub.resize();
		pub.getUser();
		pub.selectProduct();
		pub.weixin();
		pub.selCountry();
	},
	/*
	 * 获取用户信息
	 */
	getUser : function(){
		var $login=$("#login");
		
		if(base.isNull($.cookie("user"))){
			com.ifAutoLogin();
			com.data.user=eval("("+$.cookie("user")+")");
			
			$login.html('<a href="'+homeUrl+'/shop/cart.html">购物车</a><span class="line"></span><a href="'+homeUrl+'/shop/">订单</a><span class="line"></span><a id="logout">退出</a>');
			
			$("#logout").click(function(){
				com.removeSession();
			});
		}else{
			$login.html('<a class="user_icon icon_bg" href="'+pub.url.loginUrl+'">登录</a>');
		}
	},
	/*
	 * 自适应屏幕大小
	 */
	resize : function(){
		var width=0;

		$(window).ready(function(){
			res();
		});

		$(window).resize(function(){
			res();
		});

		function res(){
			width=$(this).width();

			if(width<1161) $(".main").addClass("main_min");
			else $(".main").removeClass("main_min");
		}
	},
	selCountry : function(){
		$("body").append('<ul class="selCountry" id="countryList"><li><a class="us country_icon" data-href="'+homeUrl+'/us/">United States</a></li><li><a class="uk country_icon" data-href="'+homeUrl+'/uk/">United Kingdom</a></li><li><a class="ua country_icon" data-href="http://yunmai.ua">Ukraine</a></li><li><a class="kr country_icon" data-href="http://iyunmai.kr/">대한민국</a></li><li><a class="br country_icon" data-href="https://yunmaibrasil.com.br/">Brasil</a></li><li><a class="au country_icon" data-href="'+homeUrl+'/au/">Australia</a></li><li><a class="cn country_icon" data-href="'+homeUrl+'">中国</a></li></ul>');
		
		var $countryBtn=$("#countryBtn"),$countryList=$("#countryList"),top=0,left=0,len=$countryList.children("li").length;
		
		$countryBtn.bind({
			click : function(){
				if(com.isMobile().mobile||com.isMobile().ios||com.isMobile().android||com.isMobile().iPhone||com.isMobile().iPad){
					top=$(this).offset().top;
					left=$(this).offset().left;
					$countryList.css({"top":top-(len-1)*24-6,"left":left-11}).show();
				}
			},
			mouseover : function(){
				if(!(com.isMobile().mobile||com.isMobile().ios||com.isMobile().android||com.isMobile().iPhone||com.isMobile().iPad)){
					top=$(this).offset().top;
					left=$(this).offset().left;
					$countryList.css({"top":top-(len-1)*24-6,"left":left-11}).show();
				}
			}
		});
		
		$countryList.bind({
			mouseleave : function(){
				if(!(com.isMobile().mobile||com.isMobile().ios||com.isMobile().android||com.isMobile().iPhone||com.isMobile().iPad)){
					$countryList.hide();
				}
			}
		});
		
		$countryList.find("a").click(function(){
			$.cookie("country",0,{path:'/',domain:'iyunmai.com',expires:com.setCookieTime(10)});
			window.location.href=$(this).data("href");
		})
		
		$(window).scroll(function(){
			if(com.isMobile().mobile||com.isMobile().ios||com.isMobile().android||com.isMobile().iPhone||com.isMobile().iPad){
				$countryList.hide();
			}
		})
	},
	phoneList : function(){
		$("body").append('<div class="phone_box" id="phoneBox"><div class="box"><div class="plr_35"><div class="t_1">好轻App手机兼容性列表</div><div class="t_2">好轻目前只支持以下机型系统达到 IOS 9.0 或 Android 4.3 及以上版本喔~更多机型正在努力适配中！</div><div class="t_3 icon_bg">IOS 9.0 及以上系统，搭载蓝牙 4.0</div><ul><li>iPhone X，iPhone 8 Plus，iPhone 8，iPhone 7 Plus，iPhone 7，iPhone 6s Plus，iPhone 6s，iPhone 6 Plus，iPhone 6，iPhone 5s，iPhone SE，iPad Pro，iPad 4，iPad 3，iPad Air 2，iPad Air，iPad mini 3，iPad mini 2，iPad mini，iPod touch 5</li></ul></div><div class="phoneImg"><img src="'+homeUrl+'/static/images/phoneImg.jpg" /></div><div class="plr_35"><div class="t_3 and_icon icon_bg">Android 4.3及以上系统，搭载蓝牙 4.0</div><ul><li><div class="w_100">[<label>三星</label>]</div><div class="w_700">GALAXY S2 - S6，GALAXY Note2 - Note4 ，GALAXY Grand2</div></li><li><div class="w_100">[<label>小米</label>]</div><div class="w_700">小米MIX2，小米Note3，小米6，小米Note2，小米MIX，小米5，小米Note，小米4，小米3，小米2，小米2s，红米Note 4X，小米平板</div></li><li><div class="w_100">[<label>魅族</label>]</div><div class="w_700">MX 5，MX 4 Pro，MX 4，MX 3，魅蓝，魅蓝 Note 2，魅蓝 Note</div></li><li><div class="w_100">[<label>华为</label>]</div><div class="w_700">NXT-AL10，Mate 7，Mate 2，Ascend p8 lite，Ascend p8，Ascend p7，Ascend p6 plus，Ascend p6，荣耀畅玩 4X，荣耀 3X</div></li><li><div class="w_100">[<label>vivo</label>]</div><div class="w_700">X20A，X9 Plus，X9，X5 Max，X5 Max L，X5 SL，X5 L，X5 Pro（双4G），Xshot，Xplay 3S</div></li><li><div class="w_100">[<label>oppo</label>]</div><div class="w_700">R11s，R11，R9s Plus，R9s，R9m，A59s，R7 plus，R7，R1C，N1 Mini，Find 7，N3</div></li><li><div class="w_100">[<label>Nexus</label>]</div><div class="w_700">Nexus 6，Nexus 5，Nexus 4</div></li><li><div class="w_100">[<label>努比亚</label>]</div><div class="w_700">X6，Z9 Max，Z9 mini，Z9，Z7 Mini，Z7</div></li><li><div class="w_100">[<label>更多</label>]</div><div class="w_700">锤子 T1，Xperia Z3，一加手机，U3，黄金斗士A8</div></li></ul></div><div class="phoneImg"><img style="margin-top:-300px" src="'+homeUrl+'/static/images/phoneImg.jpg"/></div><a class="close icon_bg" id="phoneClose"></a></div></div>');
		
		var $phoneBtn=$("#phoneBtn"),$phoneBox=$("#phoneBox"),$phoneClose=$("#phoneClose"),swich=true;
	
		$phoneBtn.click(function(){
			$phoneBox.show();
			setTimeout(function(){
				$phoneBox.css({"opacity":1,"transition-duration":"0.8s"});
			},10);
			$("body,html").scrollTop(0);
			$("html").css({"overflow-y":"auto"});
			$("body").css({"height":$phoneBox.children(".box").height()+40,"overflow-y":"hidden"});
		});
		
		$phoneClose.click(function(){
			if(!swich) return;
			swich=false;
			$phoneBox.css({"opacity":0,"transition-duration":"0.5s"});
			setTimeout(function(){
				$phoneBox.hide();
				swich=true;
			},500)
			$("body,html").removeAttr("style");
		});
	},
	/*
	 * 文本输入框效果
	 */
	foInp : function(obj,isWm){
		var $obj=$("#"+obj),$intBox=$("#"+obj+"Box"),$this;

		if($obj.val()) $intBox.children("i").hide();

		$obj.focus(function(){
			$intBox.addClass("input_hover");
			$("#errorMsg"+obj).hide().css({"opacity":0.3,"transition-duration":"0s","transform":"translate3d(0,0,0)"});
			$intBox.removeClass("input_error");
			
			if(isWm){
				$intBox.children("i").hide();
			}else{
				if($(this).val()) $intBox.children("i").hide();
				else $intBox.children("i").show();
			}
		}).focusout(function(){
			if(!$.trim($(this).val())) $intBox.children("i").show();
			
			$intBox.removeClass("input_hover");
		}).keydown(function(){
			$this = $(this);
			setTimeout(function(){
				if(isWm){
					$intBox.children("i").hide();
				}else{
					if($this.val()) $intBox.children("i").hide();
					else $intBox.children("i").show();
				}
			},15);
		});

		$intBox.children("i").click(function(){
			$obj.focus();
		});
	},
	/*
	 * 下拉列表效果
	 */
	downList : function(obj,h){
		var $objBox=$("#"+obj+"Box"),$objList=$("#"+obj+"List"),$obj=$("#"+obj);

		$objList.delegate("a","click",function(){
			$obj.text($(this).text());
			$objList.css({"height":0});
			$("#errorMsg"+obj).hide().css({"opacity":0.3,"transition-duration":"0s","transform":"translate3d(0,0,0)"});
			$objBox.removeClass("input_error");
		});

		$objBox.mouseover(function(){
			$(this).css({"height":"auto"});
			$objList.css({"height":"auto"});
		}).mouseout(function(){
			$(this).css({"height":h});
			$objList.css({"height":0});
		});
	},
	/*
	 * 购物车提示
	 */
	cartMsg : function(tit){
		var $cartMsg=$("#cartMsg"),width=0,swich=true;
		
		if(swich){
			swich=false;
			$cartMsg.text(tit).show().css({"margin-left":-$cartMsg.width()/2});
			$cartMsg.stop().animate({"top":135,"opacity":1},460);
		
			setTimeout(function(){
				$cartMsg.stop().animate({"top":-42,"opacity":0},280,function(){
					$(this).hide();
					swich=true;
				});
			},1800);
		}
	},
	/*
	 * 微信弹窗
	 */
	weixin : function(){
		$("body").append('<div class="weixin" id="weixinBox"><div class="con"><div class="tit">云麦好轻官方微信</div><p>打开微信，点击右上角的“ + ”，选择“扫一扫”功能，<br />对准下方二维码即可。</p><img src="https://images.iyunmai.com/home/static/images/weixin.png" /></div><a class="close icon_bg" id="weixinClose"></a></div><div class="mask mask_op" id="weixinMask"></div>');

		pub.popup("weixin");
	},
	/*
	 * 弹窗
	 */
	popup : function(obj){
		var $btn=$("#"+obj+"Btn"),$box=$("#"+obj+"Box"),$close=$("#"+obj+"Close"),$mask=$("#"+obj+"Mask"),width=0,height=0,wWidth=0,wHeight=0,scrollTop=0,top=0;
		
		$(window).resize(function(){
			set();
		});
		
		$(window).scroll(function(){
			set();
		});
	
		$btn.click(function(){
			set();
			
			$box.show().stop().animate({"opacity":1},800);
			$mask.show().stop().animate({"opacity":0.6},800);
		});
		
		$close.click(function(){
			$box.stop().animate({"opacity":0},500,function(){$(this).hide();});
			$mask.stop().animate({"opacity":0},500,function(){$(this).hide();});
		});
		
		function set(){
			wWidth = $(window).width();
			wHeight = $(window).height();
			scrollTop = $(document).scrollTop();
			width = $box.width();
			height = $box.height();
			
			top=(wHeight-height<0)?scrollTop:scrollTop+(wHeight-height)/2;
			
			$box.css({"left":(wWidth-width)/2,"top":top});
		}
	},
	/*
	 * 选择产品
	 */
	selectProduct : function(){
		$("body").append('<div class="select_product" id="selectProduct"><ul id="lightProduct"><li><a href="'+homeUrl+'/light-color2/"><div class="name">云麦好轻 Color2</div><div class="img_box i_1321"></div></a></li><li><a href="'+homeUrl+'/light2/"><div class="name">云麦好轻 2</div><div class="img_box i_16011"></div></a></li><li><a href="'+homeUrl+'/light/"><div class="name">云麦好轻</div><div class="img_box i_1301"></div></a></li><li><a href="'+homeUrl+'/light-color/"><div class="name">云麦好轻 Color</div><div class="img_box i_1311"></div></a></li><li><a href="'+homeUrl+'/light-mini2/"><div class="name">云麦好轻 mini2</div><div class="img_box i_17001"></div></a></li><li><a href="'+homeUrl+'/light-mini/"><div class="name">云麦好轻 mini</div><div class="img_box i_1501"></div></a></li><li><a href="'+homeUrl+'/light-se/"><div class="name">云麦好轻 SE</div><div class="img_box i_16801"></div></a></li></ul><ul id="accessoriesProduct"><li><a href="'+homeUrl+'/accessories/vibrationMassageDevice/"><div class="name">云麦肌肉按摩筋膜枪</div><div class="img_box i_1100"></div></a></li><li><a href="'+homeUrl+'/accessories/sportsJacket/"><div class="name">云麦训练运动内衣</div><div class="img_box i_1090"></div></a></li><li><a href="'+homeUrl+'/accessories/tightTrousers/"><div class="name">云麦训练紧身裤</div><div class="img_box i_1091"></div></a></li><li><a href="'+homeUrl+'/accessories/yogamat-cork/"><div class="name">云麦软木瑜伽垫</div><div class="img_box i_1016"></div></a></li><li><a href="'+homeUrl+'/accessories/goggles/"><div class="name">炫彩防雾泳镜</div><div class="img_box i_1080"></div></a></li><li><a href="'+homeUrl+'/accessories/floatingPlate/"><div class="name">云麦训练浮板</div><div class="img_box i_1050"></div></a></li><li><a href="'+homeUrl+'/accessories/"><div class="name">更多装备</div><div class="img_box i_more"></div></a></li></ul></div>');
		
		var $selectProduct=$("#selectProduct"),$selLightBtn=$("#selLightBtn"),$lightProduct=$("#lightProduct"),$accessoriesProduct=$("#accessoriesProduct"),$selAccessoriesBtn=$("#selAccessoriesBtn"),$header=$("#header"),stopHeaderNum="",stopProductNum="",len=0;
		
		$header.on({
			mouseover : function(){
				startHeader();
			},
			mouseout : function(){
				stopHeaderNum=setTimeout(stopHeader,5);
			}
		});
		
		$selLightBtn.on({
			mouseover : function(){
				startProduct($lightProduct,$accessoriesProduct);
			},
			mouseout : function(){
				stopProductNum=setTimeout(stopProduct,5);
			}
		});
		
		$selAccessoriesBtn.on({
			mouseover : function(){
				startProduct($accessoriesProduct,$lightProduct);
			},
			mouseout : function(){
				stopProductNum=setTimeout(stopProduct,5);
			}
		});
		
		$selectProduct.on({
			mouseover : function(){
				if(stopProductNum) clearTimeout(stopProductNum);
				if(stopHeaderNum) clearTimeout(stopHeaderNum);
			},
			mouseout : function(){
				stopProductNum=setTimeout(stopProduct,5);
				stopHeaderNum=setTimeout(stopHeader,5);
			}
		});
		
		$selectProduct.find("a").on({
			mouseover : function(){
				$selectProduct.find("a").css({"opacity":0.5});
				$(this).css({"opacity":1});
			},
			mouseout : function(){
				$selectProduct.find("a").css({"opacity":1});
			}
		});
		
		function startHeader(){
			if(base.isNull(pub.data.headerAniNum)){
				$header.removeClass(pub.data.headerAniNum==0?"head_white":"head_black").css({"background-color":"rgba(255,255,255,1)","border-bottom-color":"rgba(226,226,226,1)"});
			
				if(stopHeaderNum) clearTimeout(stopHeaderNum);
			}
		}
		
		function stopHeader(){
			if(base.isNull(pub.data.headerAniNum)){
				$header.addClass(pub.data.headerAniNum==0?"head_white":"head_black").removeAttr("style");
			}
		}
		
		function startProduct($obj1,$obj2){
			$selectProduct.css({"height":220});
			$obj2.css({"width":0,"height":0,"overflow":"hidden"});
			$obj2.children("li").removeAttr("style");
			len=$obj1.children("li").length;
			$obj1.css({"width":len*166,"height":220,"overflow":"visible","margin-left":-len*166*0.5});
			
			for(var i=0;i<len;i++){
				$obj1.children("li").eq(i).css({"transition":"opacity 0.25s ease-in-out,transform 0.8s ease","transition-delay":i*0.1+"s","opacity":1,"transform":"translate3d(0,0,0)"});
			}
			
			if(stopProductNum) clearTimeout(stopProductNum);
		}
		
		function stopProduct(){
			$selectProduct.removeAttr("style");
			$selectProduct.find("li").removeAttr("style");
			$selectProduct.find("a").removeAttr("style");
		}
	},
	/*
	 * 好轻二级导航栏
	 */
	haoqingNav : function(){
		var $haoqingNavFixed=$("#haoqingNavFixed");
		
		$(window).scroll(function(){
			if($(document).scrollTop()>86) $haoqingNavFixed.addClass("haoqing_nav_fixed");
			else $haoqingNavFixed.removeClass("haoqing_nav_fixed");
		});
	}
};